package com.tree.backend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tree.backend.common.Result;
import com.tree.backend.entity.dto.CustomerDTO;
import com.tree.backend.entity.pojo.Customer;
import com.tree.backend.entity.vo.CustomerVO;
import com.tree.backend.service.ICustomerService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;


@RestController
@RequiredArgsConstructor
@RequestMapping("/customer")
public class CustomerController {
    private final ICustomerService customerService;

    @GetMapping("/list")
    public Result<Page<CustomerVO>> getList(CustomerDTO customerDTO) {
        return customerService.getList(customerDTO);
    }


    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable("id") Long id) {
        return Result.success(customerService.removeById(id));
    }

    @PutMapping
    public Result<Boolean> update(@RequestBody CustomerDTO customerDTO) {
        // 直接转换
        Customer customer = new Customer();
        BeanUtils.copyProperties(customerDTO, customer);
        return Result.success(customerService.updateById(customer));
    }

    @PostMapping
    public Result<Boolean> add(@RequestBody CustomerDTO customerDTO) {
        Customer customer = new Customer();
        BeanUtils.copyProperties(customerDTO, customer);
        return Result.success(customerService.save(customer));
    }
}
